package com.wzcl.app.model.vo;

import com.wzcl.app.dao.model.Activity;
import com.wzcl.app.dao.model.Commodity;
import com.wzcl.app.model.common.WZCL;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@EqualsAndHashCode(callSuper = false)
@Data
public class CommodityVo extends BaseVo {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "货物ID号,Course或Material表中的Id")
    private Integer resourceId;

    @ApiModelProperty(value = "类型")
    private Integer type;
    private String typeStr;

    @ApiModelProperty(value = "商品名称")
    private String name;

    @ApiModelProperty(value = "图片")
    private List<String> pictures;

    @ApiModelProperty(value = "原价")
    private BigDecimal oldPrice;

    @ApiModelProperty(value = "现价")
    private BigDecimal currentPrice;

    @ApiModelProperty(value = "库存")
    private Integer stock;

    @ApiModelProperty(value = "已售数")
    private Integer sold;

    @ApiModelProperty(value = "是否上架")
    private Boolean sale;
    private String saleStr;

    @ApiModelProperty(value = "上架时间")
    private Date saleTime;

    @ApiModelProperty(value = "是否需要配送")
    private Boolean needDelivery;
    private String needDeliveryStr;

    @ApiModelProperty(value = "是否推荐")
    private Boolean recommend;
    private String recommendStr;

    @ApiModelProperty(value = "排序字段")
    private Integer orderNo;

    @ApiModelProperty(value = "开启规格配置")
    private Boolean spec;

    @ApiModelProperty(value = "活动")
    private List<ActivityVo> activityList;

    private CommodityVo() {
    }

    public static CommodityVo create(Commodity commodity) {
        CommodityVo commodityVo = new CommodityVo();
        BeanUtils.copyProperties(commodity, commodityVo, "pictures", "activityList");
        if (StringUtils.isNotBlank(commodity.getPictures())) {
            commodityVo.pictures = Arrays.asList(commodity.getPictures().split(","));
        } else {
            ArrayList<String> list = new ArrayList<>();
            list.add(DEFAULT_IMAGE);
            commodityVo.pictures = list;
        }
        List<Activity> activityList = commodity.getActivityList();
        if (activityList != null && activityList.size() > 0) {
            commodityVo.activityList = activityList.stream().filter(activity -> WZCL.ACTIVITY_STATE.ONLINE.equals(activity.getActivityState()))
                    .map(activity -> {
                        activity.setCommodityList(null);
                        return ActivityVo.create(activity);
                    }).collect(Collectors.toList());
        }
        commodityVo.translationValueStr();
        commodityVo.processResources();
        return commodityVo;
    }

    @Override
    public void translationValueStr() {
        Map<String, Map<Integer, String>> map = getTranslationMap();
        this.needDeliveryStr = this.needDelivery ? "是" : "否";
        this.saleStr = this.sale ? "是" : "否";
        this.recommendStr = this.recommend ? "是" : "否";
        this.typeStr = map.get(WZCL.GROUP_NAME.COMMODITY_TYPE).get(this.type);
    }

    @Override
    public void processResources() {
    }
}
